const { DataTypes } = require('sequelize');
const sequelize = require('../db');
const { saveLocalDate } = require('../../utils/moment');

exports.blogModel = sequelize.define(
	'blog',
	{
		title: {
			type: DataTypes.STRING,
			allowNull: false,
			comment: '标题'
		},
		description: {
			type: DataTypes.TEXT,
			allowNull: false,
			comment: '描述'
		},
		createDate: {
			type: DataTypes.DATE,
			allowNull: false,
			get() {
				return saveLocalDate(this.getDataValue('createDate')); // 以本地时间标准读取时间
			},
			comment: '发布日期'
		},
		scanNumber: {
			type: DataTypes.INTEGER,
			allowNull: true,
			comment: '浏览次数',
			defaultValue: 0
		},
		commentNumber: {
			type: DataTypes.INTEGER,
			allowNull: true,
			comment: '评论次数',
			defaultValue: 0
		},
		toc: {
			type: DataTypes.TEXT,
			allowNull: false,
			comment: '文章目录'
		},
		htmlContent: {
			type: DataTypes.TEXT,
			allowNull: false,
			comment: '正文'
		},
		thumb: {
			type: DataTypes.STRING,
			allowNull: false,
			comment: '缩略图'
		}
	},
	{
		freezeTableName: true,
		deletedAt: true,
		defaultScope: {
			attributes: { exclude: ['createdAt', 'updatedAt'] } // 默认排除 createdAt 和 updatedAt 字段
		}
	}
);
